package com.loong.seller.controller.backend;

import com.loong.common.constant.ShopAudit;
import com.loong.common.constant.ShopStatus;
import com.loong.common.domain.BaseQuery;
import com.loong.common.mvc.BaseBackendController;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.seller.common.backend.param.ShopParam;
import com.loong.seller.common.backend.param.ShopQueryParam;
import com.loong.seller.handler.ShopHandler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * @Description: 店铺控制器
 * @Author: hzp
 * @Date: 2020-10-26 11:45
 */
@RestController
@RequestMapping("/manage/seller/shop")
public class ShopController extends BaseBackendController {
    @Resource
    private ShopHandler shopHandler;


    /**
     * 保存店铺信息
     * @param param 店铺信息
     * @return 200
     */
    @PostMapping("save")
    public Result save(@Validated(value = {CommonInsert.class, CommonValidate.class}) ShopParam param){
        param.setId(null);
        param.setStatus(ShopStatus.UNAUDITED);//新增的店铺状态为未审核
        //保存店铺信息
        return   handlerExecutor.execute(()-> shopHandler.save(param));
    }

    /**
     * 更新店铺信息
     * @param param 店铺信息
     * @return 200
     */
    @PostMapping("update")
    public Result update(@Validated(value = {CommonUpdate.class, CommonValidate.class}) ShopParam param){
        return  handlerExecutor.execute(()->shopHandler.update(param));
    }

    /**
     *根据id删除店铺信息
     * @param id 店铺id
     */
    @PostMapping("delete")
    public Result deleteById(@RequestParam("id") Long id){
        return handlerExecutor.execute(()->shopHandler.deleteById(id));
    }

    /**
     * 根据店铺id查找店铺详情包括联系方式
     * @param shopId
     * @return
     */
    @PostMapping("detail")
    public Result findDetailByShopId(@RequestParam("shopId")Long shopId){
        return handlerExecutor.execute(()->shopHandler.findDetailByShopId(shopId));
    }


    /**
     * 根据店铺id查找店铺详情
     * @param shopId
     * @return
     */
    @PostMapping("view")
    public Result findShopByShopId(@RequestParam("shopId") Long shopId){
        return handlerExecutor.execute(()->shopHandler.findShopById(shopId));
    }


    /**
     * 分页查询
     * @param baseQuery 分页对象
     * @param param 查询条件
     * @return 店铺分页结果集
     */
    @PostMapping("pageQuery")
    public Result pageQuery(BaseQuery baseQuery, ShopQueryParam param){

        return  handlerExecutor.execute(()->shopHandler.pageQuery(baseQuery,param));
    }

    /**
     * 店铺审核通过
     * @param shopId 店铺id
     * @return 响应的结果
     */
    @PostMapping("approve")
    public Result approveShop(@RequestParam("shopId") Long shopId){
        return handlerExecutor.execute(() -> shopHandler.auditShop(shopId, ShopAudit.APPROVE));
    }

    /**
     * 店铺审核未通过
     * @param shopId 店铺id
     * @return 响应的结果
     */
    @PostMapping("reject")
    public Result rejectShop(@RequestParam("shopId") Long shopId){
        return handlerExecutor.execute(() -> shopHandler.auditShop(shopId,ShopAudit.REJECT));
    }
}
